﻿@using KRM.Core.Library
@model List<KRM.Core.v_CreatingInvoicing>
@{
    var invoices = StaticVariables.InvoiceOrders;
}
<script>
    $(document).ready(function() {
        $(".tbl-selected tbody tr").click(function() {
            $(this).addClass("row-selected");
            $(".tbl-selected tbody tr").not(this).removeClass('row-selected');
        });
    });
    function uninvoiceselected(id) {
        $(".btn-move").attr("onclick", "moveinvoice(" + id + ")");
        $(".btn-remove").removeAttr("onclick");
        $(".btn-markorder").attr("onclick", "markorder(" + id + ")");
    }
    function invoiceselected(id) {
        $(".btn-remove").attr("onclick", "removeinvoice(" + id + ")");
        $(".btn-move").removeAttr("onclick");
    }
    function moveinvoice(id) {
        App.blockUI($('body'), false);
        $.ajax({
            url: "/trade/movecreateinvoice",
            data: { id: id, ac: 0 },
            success: function (data) {
                $(".pncreatenewinvoice").html(data);
                App.unblockUI($('body'));
            }
        });
    }
    function removeinvoice(id) {
        App.blockUI($('body'), false);
        $.ajax({
            url: "/trade/movecreateinvoice",
            data: { id: id, ac: 1 },
            success: function (data) {
                $(".pncreatenewinvoice").html(data);
                App.unblockUI($('body'));
            }
        });
    }
    function markorder(id) {
        if (id == 0) {
            alert("No order selected");
        } else {
            if (confirm("Are you sure you want to mark selected order(s) as Legacy order(s)")) {
                App.blockUI($('body'), false);
                $.ajax({
                    url: "/trade/createnewinvoicemarkorder",
                    data: { id: id, accNumber: $("#hfaccNumber").val() },
                    success: function (data) {
                        $(".pncreatenewinvoice").html(data);
                        App.unblockUI($('body'));
                    }
                });
            }
        }
    }
    function createinvoice() {
        var check = @(invoices.Count > 0 ? "true" : "false");
        if (check == true) {
            App.blockUI($('body'), false);
            var consolidate = $(".cbconsolidate").is(":checked");
            var accNumber = $("#hfaccNumber").val();
            var template = $("#hftypeInvoice").val();
            window.location = "/trade/createnewinvoicetemplate?consolidate=" + consolidate + "&accNumber=" + accNumber + "&template=" + template;
            //$.ajax({
            //    url: "/trade/createnewinvoicetemplate?consolidate=" + consolidate + "&accNumber=" + accNumber + "&template=" + template,
            //    type: 'POST',
            //    contentType : "text/plain",
            //    success: function (data) {
            //        if (data == "") {
            //            window.opener.reloadpreviousinvoices(accNumber);
            //            //window.close();
            //        } else {
            //            window.opener.reloadpreviousinvoices(accNumber);
            //            App.unblockUI($('body'));
            //            //$("body").append(data);
            //        }
            //    }
            //});
        } else {
            alert("Please Add Invoices to create Order!");
        }
    }
</script>
<div class="fieldset">
    <div class="legend"><span>Invoice Orders</span></div>
    <div class="invoiceorderscroll">
        <table class="table table-bordered table-fix tbl-selected tbl-invoiceorder">
            <thead>
                <tr>
                    <th>OrderDate</th>
                    <th>OrderRef</th>
                    <th>TradeOrderRef</th>
                    <th>Status</th>
                    <th>DeliveryDate</th>
                    <th>PONumber</th>
                    <th>ProductCode</th>
                    <th>ProductName</th>
                    <th>ItemPriceExVAT</th>
                    <th>PriceListProductValue</th>
                    <th>InvoiceNumber</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in invoices)
                {
                    <tr onclick="invoiceselected(@item.TradeCustomerOrderID)">
                        <td>@item.OrderDate</td>
                        <td>@item.OrderRef</td>
                        <td>@item.TradeOrderRef</td>
                        <td>@item.Status</td>
                        <td>@item.DeliveryDate</td>
                        <td>@item.PONumber</td>
                        <td>@item.ProductCode</td>
                        <td>@item.ProductName</td>
                        <td>@item.OrderItemValue_ExVat_</td>
                        <td>@item.PriceListProductValue</td>
                        <td>@item.InvoiceNumber</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
<div class="padding10">
    <div class="row-fluid">
        <div class="span3"></div>
        <div class="span6 text-center">
            <button class="btn btn-primary btn-remove" style="width: 100px">
                <i class="icon-arrow-down"></i>
                <br />
                Remove</button>
            <button class="btn btn-primary btn-move" style="width: 100px">
                <i class="icon-arrow-up"></i>
                <br />
                Move</button>
        </div>
        <div class="span3 text-right">
            <select onchange="cbinvoicechange(this.value)">
                <option value="0" @(ViewBag.invoicevalue == 0 ? "selected=selected" : "")>EDI Imported Orders</option>
                <option value="1" @(ViewBag.invoicevalue == 1 ? "selected=selected" : "")>Not EDI Imported Orders</option>
                <option value="2" @(ViewBag.invoicevalue == 2 ? "selected=selected" : "")>All Uninvoice Orders</option>
            </select>
        </div>
    </div>
</div>
<div class="fieldset">
    <div class="legend"><span>UnInvoice Orders</span></div>
    <div class="invoiceorderscroll">
        <table class="table table-bordered table-fix tbl-selected tbl-invoiceorder">
            <thead>
                <tr>
                    <th>OrderDate</th>
                    <th>OrderRef</th>
                    <th>TradeOrderRef</th>
                    <th>Status</th>
                    <th>DeliveryDate</th>
                    <th>PONumber</th>
                    <th>ProductCode</th>
                    <th>ProductName</th>
                    <th>OrderItemValue(ExVat)</th>
                    <th>PriceListProductValue</th>
                    <th>InvoiceNumber</th>
                    <th>TradeCustomerOrderID</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr onclick="uninvoiceselected(@item.TradeCustomerOrderID)">
                        <td>@item.OrderDate</td>
                        <td>@item.OrderRef</td>
                        <td>@item.TradeOrderRef</td>
                        <td>@item.Status</td>
                        <td>@item.DeliveryDate</td>
                        <td>@item.PONumber</td>
                        <td>@item.ProductCode</td>
                        <td>@item.ProductName</td>
                        <td>@item.OrderItemValue_ExVat_</td>
                        <td>@item.PriceListProductValue</td>
                        <td>@item.InvoiceNumber</td>
                        <td>@item.TradeCustomerOrderID</td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</div>
<div class="row-fluid">
    <div class="span2">
        <input type="button" value="Mark as Legacy" class="btn btn-primary btn-markorder" onclick="markorder(0)" />
    </div>
    <div class="span2 text-right">@Model.Count Order(s)</div>
    <div class="span3">
        <label>
            <input type="checkbox" class="cbcheck cbconsolidate" />
            Consolidate Invoices
        </label>
    </div>
    <div class="span3">WARNING: Values don't Match</div>
    <div class="span2">
        <input type="button" value="Create Invoice" class="btn btn-primary" onclick="createinvoice()" />
    </div>
</div>
